<template>
  <div
    class="login_page"
    :style="
      'background:#182b58 url(' +
      bgUrl +
      ') no-repeat center center; background-size: cover;'
    "
  >
    <div class="login-box">
      <div class="logo_box">
        <h3 id="bigtitle">
          <img
            style="
              width: 70%;
              object-fit: contain;
              position: relative;
              top: 31px;
            "
            src="@/assets/images/title.png"
            alt
          />
        </h3>
        <img :src="picUrl" class="logo" alt="logo" />
      </div>
      <el-form
        v-if="show"
        class="login_form"
        :rules="rules"
        :model="loginForm"
        size="medium"
        ref="loginForm"
      >
        <caption style="margin-bottom: 25%; font-size: 22px; font-weight: 500">
          <p>绵阳市监督信息协作平台</p>
        </caption>
        <el-form-item prop="companyId">
          <!-- <TreeSelect
            :clearable="true"
            :accordion="true"
            :list="areas"
            @getValue="getValue"
          ></TreeSelect> -->
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="loginForm.username"
            :disabled="submitting"
            @keyup.enter.native="submitForm('loginForm')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="loginForm.password"
            :disabled="submitting"
            @keyup.enter.native="submitForm('loginForm')"
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input
            placeholder="请输入验证码"
            v-model="loginForm.code"
            :disabled="submitting"
            style="width: 55%; vertical-align: top"
            @keyup.enter.native="submitForm('loginForm')"
          ></el-input>
          <img
            class="img_validateCode"
            :src="verifyImgSrc"
            alt="点击切换验证码"
            title="点击切换验证码"
            @click="validatecode"
          />
        </el-form-item>
        <el-form-item class="submitButton">
          <el-button
            type="primary"
            @click="submitForm('loginForm')"
            :loading="submitting"
            >登录</el-button
          >
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <p class="copyright">-</p>
  </div>
</template>
<script>
import picUrl from "@/assets/images/login_1.png";
import bgUrl from "@/assets/images/login_bg.png";
// import TreeSelect from "@/components/treeSelect/treeSelect.vue";
export default {
  components: {
    // TreeSelect,
  },
  data() {
    return {
      show: true,
      title: process.env.VUE_APP_TITLE,
      picUrl: picUrl,
      bgUrl: bgUrl,
      submitting: false,
      BASE_URL:
        process.env.NODE_ENV !== "production"
          ? process.env.VUE_APP_BASE_API
          : process.env.VUE_APP_SERVER_URL,

      verifyImgSrc: "",
      code: 0,
      areas: [],
      loginForm: {
        username: "",
        password: "",
        code: "",
        companyId: "",
      },
      rules: {
        "loginForm.companyId": {
          required: true,
          message: "请选择区域",
        },
        "loginForm.username": { required: true, message: "请输入用户名" },
        "loginForm.password": { required: true, message: "请输入密码" },
        "loginForm.code": { required: true, message: "请输入验证码" },
      },
    };
  },
  mounted() {
    // this.verifyImgSrc = this.$BASE_URL + "/kaptcha.jpg";
    // this.getArea();
  },
  methods: {
    async getArea() {
      this.$http({ method: "get", url: "/user/companylist" }).then(
        ({ data }) => {
          this.areas = data.data;
          if (this.areas.length > 0) {
            for (let x of this.areas) {
              x.label = x.companyName;
              x.id = x.companyId;
            }
          }
        }
      );
    },
    choseArea(e) {
      this.loginForm.companyId = e[e.length - 1];
    },
    validatecode() {
      this.verifyImgSrc = this.$BASE_URL + "/kaptcha.jpg?" + this.code++;
    },
    async login() {
      const params = {
        username: this.loginForm.username,
        passport: this.loginForm.password,
        authCode: this.loginForm.code,
        department: this.loginForm.companyId,
      };
      this.submitting = true;
      try {
        this.$http({ method: "post", url: "/user/login", data: params })
          .then(({ data }) => {
            console.log(data);
            if (data.code == 0) {
              sessionStorage.setItem("token", data.data.token);
              sessionStorage.setItem("user", JSON.stringify(data.data));
              this.$router.push({
                path: "/",
              });
            } else {
              this.verifyImgSrc =
                this.$BASE_URL + "/kaptcha.jpg?" + this.code++;
            }
          })
          .catch(() => {
            this.verifyImgSrc = this.$BASE_URL + "/kaptcha.jpg?" + this.code++;
          });
        // const res = await this.$svc.login_post(params);

        // this.submitting = false;
        // if (!res) {
        //   this.verifyImgSrc = this.$BASE_URL + "/kaptcha.jpg?" + this.code++;
        //   return;
        // }
        // sessionStorage.setItem("token", res.data.data.token);
        // sessionStorage.setItem("user", JSON.stringify(res.data.data.user));
        // sessionStorage.setItem("token", "res.data.data.token");
        // sessionStorage.setItem("user", JSON.stringify({ name: "123" }));
      // eslint-disable-next-line no-empty
      } catch (error) {
      } finally {
        this.submitting = false;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login();
        } else {
          return false;
        }
      });
    },
    getValue(e) {
      this.loginForm.companyId = e;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss">
.login_page {
  .el-form {
    .el-form-item {
      margin-bottom: 20px !important;
    }
    .el-form-item__label {
      line-height: 22px !important;
    }
    .el-form-item__content {
      .el-input__inner {
        height: 40px !important;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
@import "@/assets/css/element-variables.scss";

.login_page {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;

  .login-box {
    width: 70%;
    height: 70%;
    background: linear-gradient(140deg, #55acff, #2042b8);
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    .logo_box {
      width: 60%;
      height: 100%;
      box-sizing: border-box;
      text-align: center;
      font-size: 28px;
      font-weight: bold;
      color: #edf2ff;
      letter-spacing: 1px;
      text-shadow: 5px 2px 2px #2042b8;
      display: flex;
      flex-direction: column;
      align-items: center;
      #bigtitle {
        font-size: 32px;
        white-space: nowrap;
        position: relative;
        margin-top: 20px;
      }

      .logo {
        width: 80%;
        object-fit: contain;
        position: relative;
        left: -50px;
        flex: 1;
      }
    }

    .login_form {
      width: 40%;
      caption {
        display: block;
      }
      .usb {
        img {
          width: 100%;
          height: 100%;
        }
      }
      .usb-text {
        text-align: center;
        font-size: 18px;
        margin-top: 24px;
        color: #696969de;
      }
      height: 100%;
      padding: 50px 60px;
      box-sizing: border-box;
      background-color: #fff;
      .img_validateCode {
        position: absolute;
        top: 0%;
        right: 0px;
        max-width: calc(40%);
        height: 40px;
        margin-left: 20px;
        border-radius: 4px;
        cursor: pointer;
        // background: #fff;
      }
    }
  }

  .copyright {
    position: absolute;
    bottom: 4.5%;
    color: #fff;
  }
}
.submitButton {
  .el-form-item__content {
    display: flex;
    > button {
      width: 47%;
    }
  }
  text-align: center;
  margin-top: 22px;
}
</style>
